<?php

class FormComponentIterator
{

  protected $data;

  protected $action;

  protected $index = - 1;

  public function __construct(&$data, $action = null)
  {
    $this->data = &$data;
    $this->action = $action;
  }

  /**
   *
   * @return FormComponent
   */
  public function first()
  {
    $this->reset();
    return $this->next();
  }

  /**
   *
   * @return FormComponent
   */
  public function next()
  {
    if ($this->index < count($this->data) - 1) {
      $this->index ++;
      return empty($this->action) || $this->current()->show($this->action) ? $this->current() : $this->next();
    }

    return false;
  }

  /**
   *
   * @return FormComponent
   */
  public function current()
  {
    return $this->data[$this->index];
  }

  public function reset()
  {
    $this->index = - 1;
  }

}

?>